﻿using Assets.Scripts.Interface;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class Shotgun : AProp{

    [Tooltip("射出的子弹类型")]
    public Shot shotType = Shot.RAY;
    [SerializeField, Tooltip("激光枪的伤害值")]
    public Damage lazerDamage;
    [SerializeField, Tooltip("发射子弹位置到人物中心的距离")]
    public float distance_Gun = 1f;
    [SerializeField, Tooltip("发射速度")]
    public float shotVelocity = 5f;
    [SerializeField, Tooltip("射击误差")]
    public float errorshot = 1f;
    private Vector3 shotV;//发射初速度通信
    private Transform[] playerTransform;//监视玩家位置
    private MoveControl playerStatus;//监视玩家状态


    [SerializeField, Tooltip("散弹枪一次发射的子弹")]
    private int count;
    private int i;
    public override void Click()
    {
        foreach (var p in playerTransform)
        {

            if (p == null)
            {
                continue;
            }
            Vector3 startPos = p.position + distance_Gun * MoveControl.eEect[(int)playerStatus.Dir];
            Helper.DebugPrint(startPos);
            shotV = MoveControl.eEect[(int)playerStatus.Dir] * shotVelocity;
            i = count;
            StartCoroutine(Wait(startPos,count));
        }
        

    }

    private IEnumerator Wait(Vector3 vector,int i)
    {
        i--;
        yield return 0;
        Vector3 er = Vector3.Cross(shotV, new Vector3(0, 0, 1)).normalized * UnityEngine.Random.Range(-errorshot, errorshot);
        Shots.Shot(shotV + er, vector, (int)shotType, lazerDamage);
        if(i!=0)
        {
            StartCoroutine(Wait(vector,--i));
        }
        else 
        base.ReduceCount(1);
    }

    public override void FixedAwake(){ }

    // Use this for initialization
    public override void Start () {
        base.Start();
        var ms = FindObjectsOfType<MoveControl>();
        playerTransform = new Transform[ms.Length];
        for (int i = 0; i < ms.Length; i++)
        {
            playerTransform[i] = ms[i].transform;
        }
        playerStatus = GameManager.Instance.PlayerControl;

        //初始化发射速度
        shotV = MoveControl.eEect[(int)playerStatus.Dir];
    }
	
}
